package ev.data;//need to add this line

import java.util.ArrayList;

public class Population {

	private static Population pp = null;
	private ArrayList<Individual> population;
	private ArrayList<City> cities;

	//When generate population, we must provide the list of cities
	public Population(ArrayList<City> city) {
		this.population = new ArrayList<Individual>();
		this.cities = city;
	}

	public static Population getComponent(ArrayList<City> cities)
	{
		if(pp ==null)
		{
			pp = new Population(cities);
		}
		return pp;
	}

	public void add(Individual candidate){
		candidate.calcualteQuality(cities);
		population.add(candidate);
	}
	public boolean contain(Individual candidate){
		boolean is=false;
		
		for(int i=0;i<population.size();i++){
			if(population.get(i).equals(candidate)){
				is=true;
				break;
			}
		}
		return is;
	}

	public void insert(Individual candidate){
		population.add(candidate);
	}

	public Individual remove(int position){
		return population.remove(position);
	}

	public boolean remove(Individual candidate){
		if(population.remove(candidate))
			return true;
		else
			return false;
	}
	
	public void replace(Individual candidate, int position){
		population.remove(position);
		population.add(position, candidate);
	}

	public ArrayList<City> getCities(){
		return cities;
	}

	public ArrayList<Individual> getPopulation(){
		return population;
	}
	
	public int getPopulationSize(){
		return population.size();
	}

	public Individual getIndividual(int position){
		return population.get(position);
	}
}
